Python: Add file handling support to BinaryContent for OpenAI Responses API#12258
Conversation
|
Haven't looked yet, but why can't we use BinaryContent for this? |
|
Binary is meant to be abstract (mentioned in code comments). ImageContent and FileContent derive from it. It also needed mapping to match open ai api schema spec for pdf file as base64. FileContent adapts the existing ImageContent implementation. |
2a0a1f7 to
f53e50c
Compare
|
Hi @ltwlf, can you please check your Ruff settings? There are a lot of extra line adds and such in this PR, that aren't necessary -- most of the initial changes in I see The current code has the following following 99 chars (allowed because < 120): from openai.types.responses.response_content_part_added_event import ResponseContentPartAddedEventand you're trying to commit the change that brings the first import line to 71 chars: from openai.types.responses.response_content_part_added_event import (
ResponseContentPartAddedEvent,
) |
991b27a to
3da6b35
Compare
|
@moonbox3 thanks for the feedback and you are right about the unnecessary formatting changes. I tracked down the issue - the root .vscode/settings.json was using autopep8 instead of ruff, which caused imports to be formatted differently than the project's pyproject.toml configuration. I've fixed the VSCode settings and reverted all the unrelated formatting changes. The PR now only contains the minimal changes needed for FileContent functionality, and future contributions won't have this formatting issue. |
|
@ltwlf we had a chat internally, we will eventually align the design of this with the dotnet Microsoft.Extensions.AI design, which only has BinaryContent without subclasses, so we would prefer to update BinaryContent instead of introducing something new that we then have to remove again, could you adapt this PR to do that (or create a new one)? Should be mostly adapting some of the docstrings in binarycontent and then using that instead of the filecontent for OpenAI Responses! Thanks for your efforts though, much appreciated! |
73a0e0d to
9558303
Compare
|
@eavanvalkenburg thanks for the feedback! To clarify, are you suggesting that the binary type should handle OpenAI file data by default, with ImageContent inheriting from binary and overriding methods specifically for image handling? |
196c926 to
bc8e221
Compare
|
@ltwlf exactly, eventually we might remove the ImageContent altogether, but that is breaking so we won't any time soon. |
eavanvalkenburg
left a comment
There was a problem hiding this comment.
couple of small comments, overall it looks good. Do fix the linting settings because a lot of changes come from that which is annoying!
bc8e221 to
2efa0f5
Compare
Python Test Coverage Report •
Python Unit Test Overview
|
||||||||||||||||||||||||||||||||||||||||
eavanvalkenburg
left a comment
There was a problem hiding this comment.
Looks great, thanks for the effort!
fb0c094 to
11ae8bf
Compare
|
@eavanvalkenburg now it is ready to review :) |
11ae8bf to
8f2363f
Compare
bf8ef4b to
c13deb9
Compare
c13deb9 to
1eed7a9
Compare
e55e7b4 to
8e1af11
Compare
|
I fixed some lint issues. The pre-commit hooks in my project weren’t working, so I reinstalled them. |
8e1af11 to
39f5a77
Compare
|
@moonbox3 – I’ve added the examples you requested. Could you take another look when you have a moment? I’m a bit concerned we might run into merge conflicts if this PR stays open much longer. Thanks! |
68cba06 to
be51543
Compare
- Add BinaryContent.can_read property to check data availability - Add BinaryContent.from_file() method for file-based content creation - Integrate BinaryContent with OpenAI Responses API message handling - Support multi-modal messages combining text and binary content - Enable direct file attachment in ChatMessageContent for Responses API - Update sample to demonstrate new create_client() pattern and file uploads - Add comprehensive tests for new BinaryContent functionality
be51543 to
4808f5f
Compare
…es API (microsoft#12258) ## Summary Enhances `BinaryContent` to support file handling for OpenAI Responses API, enabling file uploads through the responses agent while maintaining a provider-agnostic design. ## Changes ### BinaryContent Enhancements - **Add `can_read` property**: Indicates whether content has readable data available - **Add `from_file()` class method**: Creates BinaryContent instances from file paths with automatic base64 encoding - **Fix Unicode handling**: Prevents decode errors when processing binary files (PDFs, images, etc.) ### OpenAI Responses Agent Integration - **Add BinaryContent support**: Pattern matching case for file handling in `responses_agent_thread_actions.py` - **Correct OpenAI API format**: Uses proper `filename` and `file_data` structure with data URI format - **UUID-based filenames**: Generates appropriate filenames with mime-type extensions - **Provider-specific mapping**: File format conversion happens only in OpenAI agent code ### Testing - **Test coverage**: New tests for `can_read`, `from_file()`, and binary data handling - **Unicode error prevention**: Specific test for binary PDF-like content - **Base64 encoding verification**: Ensures proper data format for API compatibility ## Design Principles - **Provider-agnostic**: BinaryContent remains completely generic with no OpenAI-specific dependencies - **Clean separation**: OpenAI format mapping isolated to OpenAI agent files - **No FileContent class**: Enhances existing BinaryContent instead of introducing new types - **Follows existing patterns**: Similar approach to ImageContent and TextContent handling ## Usage Example ```python response = await self.agent.get_response( messages=ChatMessageContent( content="Analyse PDF", role=AuthorRole("user"), items=[BinaryContent.from_file(file_path=c":/test.pdf")], ) ) Co-authored-by: Evan Mattson <35585003+moonbox3@users.noreply.github.com>
Summary
Enhances
BinaryContentto support file handling for OpenAI Responses API, enabling file uploads through the responses agent while maintaining a provider-agnostic design.Changes
BinaryContent Enhancements
can_readproperty: Indicates whether content has readable data availablefrom_file()class method: Creates BinaryContent instances from file paths with automatic base64 encodingOpenAI Responses Agent Integration
responses_agent_thread_actions.pyfilenameandfile_datastructure with data URI formatTesting
can_read,from_file(), and binary data handlingDesign Principles
Usage Example